package com.fruit.web.vo.customer;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;
import com.fruit.controller.LoginController;
import com.fruit.service.ICouponService;
import com.fruit.utils.DateTimeUtil;
import com.fruit.utils.MessageFactory;
import com.fruit.utils.SpringFacesUtil;
import com.fruit.web.bo.impl.Coupon;
import com.fruit.web.constant.EjdConstants;
import com.fruit.web.constant.ManageBeanConstants;
import com.fruit.web.constant.SpringBeanConstants;
import com.fruit.web.vo.common.CouponPage;

@ManagedBean(name="customerCouponPage")
@SessionScoped
public class CustomerCouponPageBean implements Serializable {
	private static final long serialVersionUID = -8106629465344224755L;
	
	private CouponPage aPage;
	private CouponPage uPage;
	private CouponPage oPage;
	
	private String couponAccount;
	private String couponAccountHidden;
	private List<String> receiveCouponErrorMessages = new ArrayList<String>();
	
	
	

	public CouponPage getaPage() {
		return aPage;
	}

	public void setaPage(CouponPage aPage) {
		this.aPage = aPage;
	}

	public CouponPage getuPage() {
		return uPage;
	}

	public void setuPage(CouponPage uPage) {
		this.uPage = uPage;
	}

	public CouponPage getoPage() {
		return oPage;
	}

	public void setoPage(CouponPage oPage) {
		this.oPage = oPage;
	}

	public String getCouponAccount() {
		return couponAccount;
	}

	public void setCouponAccount(String couponAccount) {
		this.couponAccount = couponAccount;
	}

	public String getCouponAccountHidden() {
		return couponAccountHidden;
	}

	public void setCouponAccountHidden(String couponAccountHidden) {
		this.couponAccountHidden = couponAccountHidden;
	}

	public List<String> getReceiveCouponErrorMessages() {
		return receiveCouponErrorMessages;
	}

	public void setReceiveCouponErrorMessages(
			List<String> receiveCouponErrorMessages) {
		this.receiveCouponErrorMessages = receiveCouponErrorMessages;
	}

	public CustomerCouponPageBean(){
		LoginCustomerPageBean loginCustomerPage = (LoginCustomerPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.LOGIN_CUSTOMER_PAGE_BEAN_NAME);
		Coupon paraA = new Coupon();
		paraA.setStatus(EjdConstants.COUPON_STATUS_ACTIVE);
		paraA.setCustomerId(loginCustomerPage.getCustomerInfo().getCustomerId());
		aPage = new CouponPage(1,10,paraA);
		aPage.getPageDatas();
		Coupon paraU = new Coupon();
		paraU.setStatus(EjdConstants.COUPON_STATUS_USED);
		paraU.setCustomerId(loginCustomerPage.getCustomerInfo().getCustomerId());
		uPage = new CouponPage(1,10,paraU);
		uPage.getPageDatas();
		Coupon paraO = new Coupon();
		paraO.setStatus(EjdConstants.COUPON_STATUS_OVERDUE);
		paraO.setCustomerId(loginCustomerPage.getCustomerInfo().getCustomerId());
		oPage = new CouponPage(1,10,paraO);
		oPage.getPageDatas();
	}
	
	public String selectaPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getaPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.getaPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getaPage().getPageDatas();
		return result;	
	}
	public String selectuPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getuPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.getuPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getuPage().getPageDatas();
		return result;	
	}
	public String selectoPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getoPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.getoPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getoPage().getPageDatas();
		return result;	
	}
	
	public String receiveCoupon(){
		String couponAccountHidden= (String)SpringFacesUtil.getRequestParameter("viewCouponForm:couponAccountHidden");
		this.setCouponAccountHidden(couponAccountHidden);
		this.getReceiveCouponErrorMessages().clear();
		if (!(LoginController.loggedIn())) {
			return EjdConstants.GO_TO_LOGIN + "?faces-redirect=true";
		}
		Coupon coupon = null;
		if (null == this.getCouponAccountHidden() || "".equals(this.getCouponAccountHidden().trim())) {
			String couponMustInputN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"coupon.account.must.input.message", FacesMessage.SEVERITY_ERROR).getDetail();
			this.getReceiveCouponErrorMessages().add(couponMustInputN18);
			return null;
		}
		ICouponService couponService = (ICouponService)SpringFacesUtil.getSpringBean(SpringBeanConstants.COUPON_SERVICE_BEAN_NAME);
		couponService.setEntityClass(Coupon.class);
		couponService.setPKClass(Long.class);
		coupon = (Coupon) couponService.findUniqueBy("couponAccount",this.getCouponAccountHidden().trim());
		if (null == coupon) {
			String couponNotExistN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"coupon.not.exist.message", FacesMessage.SEVERITY_ERROR).getDetail();
			this.getReceiveCouponErrorMessages().add(couponNotExistN18);
			return null;
		} else {
			if (null == coupon.getStatus() || "".equals(coupon.getStatus())) {
				String couponUncertaintyN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"coupon.status.uncertainty.message", FacesMessage.SEVERITY_ERROR).getDetail();
				this.getReceiveCouponErrorMessages().add(couponUncertaintyN18);
				return null;
			}
			if (EjdConstants.COUPON_STATUS_USED.equals(coupon.getStatus())) {
				String couponUsedN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"coupon.status.used.message", FacesMessage.SEVERITY_ERROR).getDetail();
				this.getReceiveCouponErrorMessages().add(couponUsedN18);
				return null;
			}
			if (EjdConstants.COUPON_STATUS_DISCARDED.equals(coupon.getStatus())) {
				String couponDiscardedN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"coupon.status.discarded.message", FacesMessage.SEVERITY_ERROR).getDetail();
				this.getReceiveCouponErrorMessages().add(couponDiscardedN18);
				return null;
			}
			if (coupon.getAvailableDate().before(DateTimeUtil.getCurrentTimestamp()) || EjdConstants.COUPON_STATUS_OVERDUE.equals(coupon.getStatus())) {
				String couponOverdueN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"coupon.status.overdue.message", FacesMessage.SEVERITY_ERROR).getDetail();
				this.getReceiveCouponErrorMessages().add(couponOverdueN18);
				return null;
			}
			if (!(EjdConstants.COUPON_STATUS_ACTIVE.equals(coupon.getStatus()))) {
				String couponNotActiveN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"coupon.status.not.active.message", FacesMessage.SEVERITY_ERROR).getDetail();
				this.getReceiveCouponErrorMessages().add(couponNotActiveN18);
				return null;
			} else {
				 if (null != coupon.getCustomerId() && !("".equals(coupon.getCustomerId()))) {
					 
					 LoginCustomerPageBean loginCustomerPage = (LoginCustomerPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.LOGIN_CUSTOMER_PAGE_BEAN_NAME);
					 if (loginCustomerPage.getCustomerInfo().getCustomerId().equals(coupon.getCustomerId())) {
						 String couponOwnedByYourSelfN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"coupon.owned.yourself.message", FacesMessage.SEVERITY_ERROR).getDetail();
						 this.getReceiveCouponErrorMessages().add(couponOwnedByYourSelfN18);
						 return null;
					 } else {
						 String couponOwnedBySomeBodyN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"coupon.owned.somebody.message", FacesMessage.SEVERITY_ERROR).getDetail();
						 this.getReceiveCouponErrorMessages().add(couponOwnedBySomeBodyN18);
						 return null;
					 }
					 
				 } else {/*
					this.setCouponChecked(true);
					this.setCouponErrorMessage("");
					this.setCouponScore(coupon.getCouponScore());
					this.getOrder().setCouponScore(this.getCouponScore());
					this.getOrder().setCouponAccount(this.getCouponAccount());
					return null;*/
					 return null;
				}
			}
		}
	}
	public void changeCouponAccount(ValueChangeEvent e){
		String newValue = (String) e.getNewValue();
		this.setCouponAccount(newValue);
		this.getReceiveCouponErrorMessages().clear();
	}

}
